package ch.bfh.medinfo.smartsmsspeaker.device;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.util.Log;


//Intent-filters needed:
//<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
//<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
//
//If unsure check:
//http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
//http://code.google.com/p/android-battery-monitor/source/browse/trunk/Battery+Monitor/src/com/mobware4u/batterymonitor/BatteryService.java?r=3

public class PowerConnectionReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		if(intent==null) return;
		
		boolean isCharging = intent.getAction().equals(
				Intent.ACTION_POWER_CONNECTED);

		if (!isCharging) {
			Log.i("ch.bfh.medinfo", "Dis-charching");
		}
		if (isCharging) {
			Intent iintent = context.registerReceiver(null, new IntentFilter(
					Intent.ACTION_BATTERY_CHANGED));
			int status = iintent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
			int chargePlug = iintent.getIntExtra(BatteryManager.EXTRA_PLUGGED,
					-1);
			
			Log.i("ch.bfh.medinfo", "Charching: [" + chargePlug + "]");
			
			if(chargePlug == BatteryManager.BATTERY_PLUGGED_USB)
				Log.i("ch.bfh.medinfo", "Charching at USB-adapter");
			if(chargePlug == BatteryManager.BATTERY_PLUGGED_AC)
				Log.i("ch.bfh.medinfo", "Charching at AC-adapter");
				
			//if(chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS)
			//Log.i("ch.bfh.medinfo", "Charching at Wireless-adapter");
			
			//Exercise: Determine the charge-level (in Percent) of the battery!!

		}
	}
}
